package zrar;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import zrar.panel.MainPanel;
import zrar.util.SystemUtil;

public class MainFrame {

	private JFrame frame;
	private MainPanel mainPanel;

	static final int WIDTH = 1024;
	static final int HEIGHT = 768;

	public MainFrame() {
		frame = new JFrame();
		frame.setTitle("幸运大抽奖");
		//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		try {
			// 将LookAndFeel设置成Windows样式
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");			
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		mainPanel = new MainPanel();
		
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		toolkit.addAWTEventListener( new MyKeyBoardListener(), AWTEvent.KEY_EVENT_MASK);
		
		frame.add(mainPanel);
		frame.setSize(WIDTH, HEIGHT);
		SystemUtil.showInCenter(frame);
		frame.setUndecorated(true);  
		frame.setVisible(true);
	}
	
	class MyKeyBoardListener implements AWTEventListener {
		public void eventDispatched(AWTEvent e) {
			if (e.getID() == KeyEvent.KEY_PRESSED) {
				KeyEvent evt = (KeyEvent) e;
				if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
					Run_Action();
				}else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE){
					SystemUtil.close_Action();
				}
			}
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new MainFrame();
			}
		});
	}

	public void Run_Action() {
		if(mainPanel.getRunning()){
			mainPanel.stop_Action();
		}else{
			mainPanel.run_Action();
		}		
	}

}
